#include "../inc/mydbop.h"

/**
 * @brief db_init 词典数据库初始化
 * @param 无
 * @retval 词典数据库指针
 */
sqlite3 *db_init()
{
    sqlite3 *db = NULL;
    char *err = NULL;
    if (SQLITE_OK != sqlite3_open("../mydb.db", &db))
    {
        // error
        printf("sqlite3_open error : %s\n", sqlite3_errmsg(db));
        exit(-1);
    }
    char *sql = "create table if not exists staffinfo(name text primary key,old int,department text,salary int)";
    if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &err))
    {
        // error
        printf("sqlite3_exec error : %s\n", err);
        // return NULL;
        exit(-1);
    }

    sql = "create table if not exists usrinfo(name text,usrname text primary key,password text)";
    if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &err))
    {
        // error
        printf("sqlite3_exec error : %s\n", err);
        // return NULL;
        exit(-1);
    }

    sql = "create table if not exists managerinfo(name text,usrname text primary key,password text)";
    if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &err))
    {
        // error
        printf("sqlite3_exec error : %s\n", err);
        // return NULL;
        exit(-1);
    }

    sqlite3_free(err);
    return db;
}

int db_insert_usr_info(sqlite3 *db, char *name, char *usrname, char *password) // 数据库插入用户注册信息
{
    char sql[256] = {0};
    sprintf(sql, "insert into usrinfo values('%s','%s','%s')", name, usrname, password);
    // printf("%s\n",sql); //demo
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        // error
        if (SQLITE_CONSTRAINT == res) // 用这个宏判断是否插入了重复的信息
        {
            printf("用户名已存在\n");
            return -2;
        }
        else
        {
            printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
            return -1;
        }
    }
    return 0;
}

int call_back_verify(void *login_info, int ncolumn, char **f_value, char **f_name)
{
    if (!strcmp(((login_info_t *)(login_info))->usrname, f_value[1]))
    {
        if (!strcmp(((login_info_t *)(login_info))->password, f_value[2]))
        {
            // 说明登录成功
            ((login_info_t *)(login_info))->if_true = 1;
        }
    }
    return 0;
}

int call_back_search(void *search_info, int ncolumn, char **f_value, char **f_name)
{
    if (!strcmp(((search_info_t *)search_info)->name, f_value[0]))
    {
        // 查询到了
        ((search_info_t *)search_info)->if_true = 1;

        strcpy(((search_info_t *)search_info)->staff_info.name, f_value[0]);
        ((search_info_t *)search_info)->staff_info.old = atoi(f_value[1]);
        strcpy(((search_info_t *)search_info)->staff_info.department, f_value[2]);
        ((search_info_t *)search_info)->staff_info.salary = atoi(f_value[3]);
    }
    return 0;
}

int db_verify(sqlite3 *db, login_info_t login_info)
{
    char *sql = "select * from usrinfo";

    if (SQLITE_OK != sqlite3_exec(db, sql, call_back_verify, &login_info, NULL))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    if (login_info.if_true)
    {
        return 1; // 登录成功
    }
    else
        return 0; // 登录失败
}

int db_search(sqlite3 *db, search_info_t *search_info) // 查询
{
    char *sql = "select * from staffinfo";
    if (SQLITE_OK != sqlite3_exec(db, sql, call_back_search, search_info, NULL))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    if (!search_info->if_true) // 说明没查到
    {

        return -1;
    }
    // 查到了

    return 0;
}

int db_insert_staff_info(sqlite3 *db, trans_dbinfo_t staff_info)
{
    char sql[256] = {0};
    sprintf(sql, "insert into staffinfo values('%s',%d,'%s',%d)", staff_info.name, staff_info.old,
            staff_info.department, staff_info.salary);
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        // error
        if (SQLITE_CONSTRAINT == res) // 用这个宏判断是否插入了重复的信息
        {
            // printf("用户名已存在\n");
            return -2;
        }
        else
        {
            printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
            return -1;
        }
    }
    return 0;
}

int db_modify_staff_info(sqlite3 *db, trans_dbinfo_t staff_info)
{
    char sql[256] = {0};
    sprintf(sql, "update staffinfo set old=%d,department='%s',salary=%d where name='%s'", staff_info.old,
            staff_info.department, staff_info.salary, staff_info.name);
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    return 0;
}

int db_delete_staff_info(sqlite3 *db, search_info_t *search_info) 
{
    // 先查一遍有没有
    if (db_search(db, search_info) == -1)
    {
        return -1;
    }

    char sql[256] = {0};
    sprintf(sql, "delete from staffinfo where name='%s'", search_info->name);
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    return 0;
}

int call_back_select_all(void *point, int ncolumn, char **f_value, char **f_name)
{
    mylist_t *last = ((mylist_nb_t *)point)->last;
    // mylist_t* last = *(mylist_t**)point;
    char buf[64] = {0};
    sprintf(buf, "%s %s %s %s",
            f_value[0], f_value[1], f_value[2], f_value[3]);

    ((mylist_t *)last)->next = malloc(sizeof(mylist_t));
    if (NULL == ((mylist_t *)last)->next)
    {
        // error
        // 内存不足
    }
    ((mylist_t *)last)->next->next = NULL;
    memset(((mylist_t *)last)->next->data, 0, sizeof(((mylist_t *)last)->next->data));
    strcpy(((mylist_t *)last)->next->data, buf);

    ((mylist_nb_t *)point)->last = (((mylist_t *)last))->next;
    ((mylist_nb_t *)point)->count++;

    return 0;
}

int db_select_all(sqlite3 *db, int connect_fd)
{
    char *sql = "select * from staffinfo";
    char *err = NULL;

    mylist_t *head = malloc(sizeof(mylist_t));
    if (NULL == head)
    {
        // error
        // 内存不足
    }
    head->next = NULL;
    memset(head->data, 0, sizeof(head->data));

    mylist_nb_t last;
    last.last = head;
    last.count = 0;

    if (SQLITE_OK != sqlite3_exec(db, sql, call_back_select_all, &last, &err))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    mylist_t *temp = head;

    char *send_buf = (char *)malloc((last.count + 1) * 40 + 2); // 预留协议相关数据的大小 以及多一部分的空间
    memset(send_buf, 0, sizeof(send_buf));

    sprintf(send_buf, "%d", select_all_verify_protocol);
    if (temp->next != NULL)
    {
        while (temp->next->next != NULL) // 之所以这样 是为了不发送最后一个空的数据
        {
            strcat(send_buf, temp->next->data);
            strcat(send_buf, " ");
            temp = temp->next;
        }

        strcat(send_buf, temp->next->data);
    }
    else
    {
        strcat(send_buf, " ");
    }

    send(connect_fd, send_buf, (last.count + 1) * 40 + 2, 0);

    while (head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }

    free(send_buf);

    return 0;
}

int db_verify_manager(sqlite3 *db, login_info_t login_info)
{
    char *sql = "select * from managerinfo";

    if (SQLITE_OK != sqlite3_exec(db, sql, call_back_verify, &login_info, NULL))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    if (login_info.if_true)
    {
        return 1; // 登录成功
    }
    else
        return 0; // 登录失败
}

int register_usr(trans_usrinfo_t *register_info, sqlite3 *db)
{
    char sql[256] = {0};
    sprintf(sql, "insert into usrinfo values('%s','%s','%s')", register_info->name,
            register_info->usrname, register_info->password);
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        // error
        if (SQLITE_CONSTRAINT == res) // 用这个宏判断是否插入了重复的信息
        {
            // printf("用户名已存在\n");
            return -2;
        }
        else
        {
            printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
            return -1;
        }
    }
    return 0;
}

int delete_usr(trans_usrinfo_t *register_info, sqlite3 *db)
{
    //先查一遍有没有
    search_info_t mysearch;
    mysearch.if_true = 0;
    strcpy(mysearch.name, register_info->name);
    if (db_search(db, &mysearch) == -1)
    {
        return -1;
    }

    char sql[256] = {0};
    sprintf(sql, "delete from usrinfo where name='%s'", register_info->name);
    int res = 0;
    if (SQLITE_OK != (res = sqlite3_exec(db, sql, NULL, NULL, NULL)))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    return 0;
}

int call_back_select_usr(void *point, int ncolumn, char **f_value, char **f_name)
{
    mylist_t *last = ((mylist_nb_t *)point)->last;
    // mylist_t* last = *(mylist_t**)point;
    char buf[64] = {0};
    sprintf(buf, "%s %s %s",
            f_value[0], f_value[1], f_value[2]);

    ((mylist_t *)last)->next = malloc(sizeof(mylist_t));
    if (NULL == ((mylist_t *)last)->next)
    {
        // error
        // 内存不足
    }
    ((mylist_t *)last)->next->next = NULL;
    memset(((mylist_t *)last)->next->data, 0, sizeof(((mylist_t *)last)->next->data));
    strcpy(((mylist_t *)last)->next->data, buf);

    // *(mylist_t**)point = ((mylist_t*)last)->next;
    ((mylist_nb_t *)point)->last = (((mylist_t *)last))->next;
    ((mylist_nb_t *)point)->count++;

    return 0;

}

int db_select_allusr(sqlite3* db,int connect_fd)
{
    char *sql = "select * from usrinfo";
    char *err = NULL;
    
    mylist_t *head = malloc(sizeof(mylist_t));
    if (NULL == head)
    {
        // error
        // 内存不足
    }
    head->next = NULL;
    memset(head->data, 0, sizeof(head->data));

    mylist_nb_t last;
    last.last = head;
    last.count = 0;

    // 得知道有几个链表现在 然后后面分配多少的空间

    if (SQLITE_OK != sqlite3_exec(db, sql, call_back_select_all, &last, &err))
    {
        printf("sqlite3_exec error: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    mylist_t *temp = head;

    char *send_buf = (char *)malloc((last.count + 1) * 40 + 2); // 预留协议相关数据的大小 以及多一部分的空间
    memset(send_buf, 0, sizeof(send_buf));

    sprintf(send_buf, "%d", select_allusr_verify_protocol);
    if (temp->next != NULL)
    {
        while (temp->next->next != NULL) // 之所以这样 是为了不发送最后一个空的数据
        {
            strcat(send_buf, temp->next->data);
            strcat(send_buf, " ");
            temp = temp->next;
        }

        strcat(send_buf, temp->next->data);
    }
    else
    {
        strcat(send_buf, " ");
    }

    send(connect_fd, send_buf, (last.count + 1) * 40 + 2, 0);

    while (head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }

    free(send_buf);
}